<?php slot("page", "home"); ?>

<!--h1>Home</h1-->

<?php
    if (count($userSources) == 0) {
        echo "<div class='alert alert-info'><strong>This page shows an overview of your sources</strong>, but you currently have no sources configured. Head to the <a href='" . url_for("feedback/addSource") . "'>add source</a> page to add your apps.</div>";
    }
    else {
?>
        <div class="row">
            <?php
                $count = 0;
                foreach ($userSources as $userSource) {
                    $count++;
            ?>
                    <div class="span4">
                        <div class="well source">
                            <?php echo DisplayHelper::printIconWithRoundedCorners($userSource->Source->getFinalIconUrl()) ?>
                            <h3><?php echo $userSource->getDisplayName() ?>  <?php echo DisplayHelper::createProviderIconHtml($userSource->Source->getProvider()) ?></h3>
                            
                            <?php
                            
                                $feedbackCount = Doctrine_Core::getTable('Feedback')
                                    ->createQuery('f')
                                    ->where('f.source_id = ?', $userSource->getSourceId())
                                    ->count();
                            
                            ?>
                            
                            <div class="clear"></div>
                            
                                <?php if (!$userSource->Source->hasEverBeenUpdated()) { ?>
                                    <p style="float: left;">Watch the status bar (below) for update details...</p>
                                <?php } else { ?>
                                    <p style="float: left;"><?php echo ($feedbackCount > 0 ? $feedbackCount : "No") ?> item<?php echo ($feedbackCount != 1 ? "s" : "") ?> of feedback</p>
                                    <p style="float: right;"><?php echo DisplayHelper::createRatingHtml($userSource->Source->getRating()) ?></p>
                                <?php } ?>
                            
                            <div class="clear"></div>
                            
                            <?php
                                $lastSuccessfulRefreshRun = $userSource->Source->getLastSuccessfulRefreshRun();
                                if ($lastSuccessfulRefreshRun) {
                                    //echo "<p><strong>Latest update:</strong> " . DateHelper::fuzzyDateDetailed($lastSuccessfulRefreshRun->getEndDate())
                                    //    . " (" . ($lastSuccessfulRefreshRun->getFeedbackCount() > 0 ? $lastSuccessfulRefreshRun->getFeedbackCount() : "No") . " new feedback item(s) added)</p>";
                                }
                            ?>
                            <div>
                                <a href="<?php echo url_for('feedback/list') . '?sourceId=' . $userSource->Source->getId() ?>"><button class="btn" <?php if (!$userSource->Source->hasEverBeenUpdated()) echo "disabled"; ?>><i class="icon-list"></i> List feedback</button></a>
                                <a href="<?php echo url_for('feedback/list') . '?sourceId=' . $userSource->Source->getId() . "&view=graph" ?>"><button class="btn" <?php if (!$userSource->Source->hasEverBeenUpdated()) echo "disabled"; ?>><i class="icon-signal"></i> Build graph</button></a>
                                <?php
                                    $buttonText = "<a href='" . url_for('feedback/doRequestUpdate?sourceId=' . $userSource->Source->getId()) . "'><i class=\"icon-refresh\"></i> Request update</a>";
                                    $disabled = false;
                                    if ($userSource->Source->getRunStatus() == Source::RUN_STATUS_RUNNING) {
                                        $buttonText = "<span class=\"no-click\">Update running, please wait...</span>";
                                        $disabled = true;
                                    }
                                    else if ($userSource->Source->getRunStatus() == Source::RUN_STATUS_REQUESTED) {
                                        $buttonText = "<span class=\"no-click\">Update requested, please wait...</span>";
                                        $disabled = true;
                                    }
                                    else if ($userSource->Source->getAllowUpdates() == 0) {
                                        $buttonText = "<span class=\"no-click\">Updates disabled</span>";
                                        $disabled = true;
                                    }
                                ?>
                                <div class="dropdown">
                                    <a class="dropdown-toggle" data-toggle="dropdown" href="#"><button class="btn">More <b class="caret"></b></button></a>
                                    <ul id="menu1" class="dropdown-menu" role="menu">
                                        <li role="presentation"><?php echo $buttonText; ?></li>
                                        <li role="presentation"><a href="#" data-source-id="<?php echo $userSource->Source->getId() ?>" class="show-update-history"><i class="icon-time"></i> Show update history</a></li>
                                        <li role="presentation" class="divider"></li>
                                        <li role="presentation"><a href="#" data-user-source-id="<?php echo $userSource->getId() ?>" data-nickname="<?php echo $userSource->getDisplayName() ?>" class="edit-nickname"><i class="icon-pencil"></i> Edit nickname</a></li>
                                        <li role="presentation"><a href="#" data-user-source-id="<?php echo $userSource->getId() ?>" class="delete-source"><i class="icon-trash"></i> Delete this source</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
            <?php
                    if ($count % 3 == 0) {
                        echo "</div><div class=\"row\">";
                    }
                }
            ?>
                <?php if ($user->isAllowedToAddMoreSources()) : ?><a href="<?php echo url_for('feedback/addSource') ?>"><?php endif; ?>
                    <div class="span4">
                        <div class="well source add-source">
                            <img src="/img/circle-plus-white.png" /> 
                            <h3>Add another source</h3>
                            <p>See feedback your other apps, or perhaps keep track of some of your competitors' apps.</p>
                        </div>
                    </div>
                <?php if ($user->isAllowedToAddMoreSources()) : ?></a><?php endif; ?>
                
                <?php if (!$user->isAllowedToAddMoreSources() && $user->isEligibleForUpgrade()) : ?>
                    <script type="text/javascript">
                        $(".add-source").click(function() {
                            showUpgradeModal("Sorry, you have reached the maximum amount of allowed sources for a free account!");
                        });
                    </script>
                <?php endif; ?>
            
        </div>
        
        <!-- Update history modal popup -->
        <div class="modal hide fade" id="update-history-modal" style="margin-top: -200px;">
            <div class="modal-header">
                <h3></h3>
            </div>
            <div class="modal-body main">
                
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal">Close</button>
            </div>
        </div>
    
        <script type="text/javascript">
            $(".show-update-history").click(function() {
                var sourceId = $(this).attr("data-source-id");
                
                $.getJSON(
                    "<?php echo url_for('feedback/updateHistory') ?>?sourceId=" + sourceId,
                    null,
                    function(data) {
                        $("#update-history-modal h3").empty();
                        $("#update-history-modal .modal-body").empty();
                        
                        $("#update-history-modal h3")
                            .append(data.result.userSource.iconHtml)
                            .append($("<span />").html("Updates for " + data.result.userSource.name + " "))
                            .append(data.result.userSource.providerIconHtml)
                            .append($("<div />").addClass("clear"));
                        
                        var table =
                            $("<table />")
                                .addClass("table")
                                .addClass("table-striped")
                                .append(
                                    $("<thead />")
                                        .append($("<th />").html("Status"))
                                        .append($("<th />").html("Date"))
                                )
                                .append($("<tbody />"));
                        $(data.result.runs).each(function(index, element) {
                            var status = "";
                            var date = "";
                            
                            if (element.complete) {
                                status = "Complete (" + (element.feedbackCount > 0 ? element.feedbackCount : "no") + " item(s) added)";
                                date = element.endDateFuzzy + " (took " + element.timeTaken + " seconds)";
                            }
                            else if (element.running) {
                                status = "<img src='/img/loading.gif' /> Running now, please be patient...";
                                date = element.startDateFuzzy;
                            }
                            else if (element.requested) {
                                status = "Requested";
                                date = element.requestedDateFuzzy;
                            }
                            
                            $("tbody", table).append(
                                $("<tr />")
                                    .append($("<td>").html(status))
                                    .append($("<td>").html(date))
                            );
                        });
                        
                        $("#update-history-modal .modal-body").append(table);
                        
                        $("#update-history-modal").modal();
                    }
                );
            });
        </script>

        <!-- Delete source modal popup -->
        <div class="modal hide fade" id="delete-source-modal" style="margin-top: -200px;">
            <div class="modal-header">
                <h3></h3>
            </div>
            <div class="modal-body main">
                <p>Are you sure you want to delete this source? You will no longer see it and you will lose all associated tagging information. This cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <button id="delete-cancel" class="btn" data-dismiss="modal">Cancel</button>
                <button id="delete-submit" class="btn btn-danger">Delete</button>
            </div>
        </div>
    
        <script type="text/javascript">
            $(".delete-source").click(function() {
                var userSourceId = $(this).attr("data-user-source-id");
                
                $("#delete-source-modal h3").html("Delete source?");
                $("#delete-source-modal .modal-body").show();
                $("#delete-submit").show();
                $("#delete-cancel").html("Cancel");
                
                $("#delete-submit").unbind("click.delete");
                $("#delete-submit").bind("click.delete", function() {
                   $.getJSON(
                        "<?php echo url_for('feedback/doDeleteSource') ?>?id=" + userSourceId,
                        null,
                        function(data) {
                            if (data.success) {
                                $("#delete-source-modal h3").html("<i class=\"icon-ok\"></i> Delete successful");
                                $("#delete-cancel").html("Done");
                                $('#delete-source-modal').on('hide', function () {
                                    $("#main").mask("Reloading...");
                                    window.location.reload();
                                });
                            }
                            else {
                                $("#delete-source-modal h3").html("Delete failed");
                                $("#delete-cancel").html("Close");
                            }
                            
                            $("#delete-source-modal .modal-body").hide();
                            $("#delete-submit").hide();
                        }
                    );
                });
                
                $("#delete-source-modal").modal();
            });
        </script>

        <!-- Edit nickname source modal popup -->
        <div class="modal hide fade" id="edit-nickname-modal" style="margin-top: -200px;">
            <div class="modal-header">
                <h3></h3>
            </div>
            <div class="modal-body main">
                <span>Update this source's nickname: </span>
                <input id="nickname" type="text" placeholder="" />
            </div>
            <div class="modal-footer">
                <button id="edit-nickname-cancel" class="btn" data-dismiss="modal">Cancel</button>
                <button id="edit-nickname-submit" class="btn btn-primary">Save</button>
            </div>
        </div>
    
        <script type="text/javascript">
            $(".edit-nickname").click(function() {
                var userSourceId = $(this).attr("data-user-source-id");
                var nickname = $(this).attr("data-nickname");
                
                $("#edit-nickname-modal h3").html("Edit source nickname");
                $("#edit-nickname-modal .modal-body").show();
                $("#nickname").attr("placeholder", nickname);
                $("#edit-nickname-submit").show();
                $("#edit-nickname-cancel").html("Cancel");
                
                $("#edit-nickname-submit").unbind("click.editNickname");
                $("#edit-nickname-submit").bind("click.editNickname", function() {
                   $.getJSON(
                        "<?php echo url_for('feedback/doEditNickname') ?>?id=" + userSourceId + "&name=" + $("#nickname").val(),
                        null,
                        function(data) {
                            if (data.success) {
                                $("#edit-nickname-modal h3").html("<i class=\"icon-ok\"></i> Edit successful");
                                $("#edit-nickname-cancel").html("Done");
                                $('#edit-nickname-modal').on('hide', function () {
                                    $("#main").mask("Reloading...");
                                    window.location.reload();
                                });
                            }
                            else {
                                $("#edit-nickname-modal h3").html("Edit failed");
                                $("#edit-nickname-cancel").html("Close");
                            }
                            
                            $("#edit-nickname-modal .modal-body").hide();
                            $("#edit-nickname-submit").hide();
                        }
                    );
                });
                
                $("#edit-nickname-modal").modal();
            });
        </script>
<?php } ?>






